#!/bin/bash
###############################################################
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
# installer is licensed under Mulan PSL v2.
# You can use this software according to the terms and conditions of the Mulan PSL v2.
# You may obtain a copy of Mulan PSL v2 at:
#          http://license.coscl.org.cn/MulanPSL2
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
# EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
# MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
# See the Mulan PSL v2 for more details.
###############################################################

if [ "x$(uname)" != "xLinux" ]; then
  echo ""
  echo 'Warning: Non-Linux operating systems are not supported!'
  exit 1
fi

if [ -z "${ARCH}" ]; then
  case "$(uname -m)" in
  x86_64)
    ARCH=amd64
    ;;
  aarch64*)
    ARCH=arm64
    ;;
  *)
    echo "${ARCH}, isn't supported"
    exit 1
    ;;
  esac
fi

if [ -z "${VERSION}" ]; then
  VERSION="latest"
fi

if [[ ${VERSION} =~ ^[0-9a-zA-Z\.-]+$ ]]; then
  echo ""
else
  echo "Version information incorrect. Set VERSION env var and re-run. For example: export VERSION=1.0.0"
  exit 1
fi

if [ "${VERSION}" != "latest" ]; then
  VERSION="v${VERSION}"
fi

if [ -f "bkeadm_linux_${ARCH}" ]; then
  echo ""
  echo "bkeadm ${VERSION} already exists in current directory!"
  mv bkeadm_linux_${ARCH} bkeadm_linux_${ARCH}_bk
  echo "the original bkeadm_linux_${ARCH} has been renamed to bkeadm_linux_${ARCH}_bk"
  echo ""
fi

DOWNLOAD_URL="https://openfuyao.obs.cn-north-4.myhuaweicloud.com/openFuyao/bkeadm/releases/download/${VERSION}/bkeadm_linux_${ARCH}"

echo "Downloading bkeadm ${VERSION} from ${DOWNLOAD_URL} ..."
echo ""

curl -fsLO "$DOWNLOAD_URL"
if [ $? -ne 0 ]; then
  echo ""
  echo "Failed to download bkeadm ${VERSION} !"
  echo ""
  echo "Please verify the version you are trying to download."
  echo ""
  exit 1
fi

echo ""
echo "bkeadm ${VERSION} Download Complete!"
echo ""

if [ -f "/usr/bin/bke" ]; then
  echo ""
  echo "bke command already exists in /usr/bin!"
  mv /usr/bin/bke /usr/bin/bke_bk -f
  echo "the original bke has been renamed to bke_bk"
  echo ""
fi

chmod +x bkeadm_linux_${ARCH}
mv bkeadm_linux_${ARCH} /usr/bin/bke -f
hash -r >> /dev/null

echo ""
echo "bke has been installed to /usr/bin"
echo ""
